//
//  MeInfoViewController.m
//  QT
//
//  Created by 甘世清 on 2025/1/3.
//

#import "MeInfoViewController.h"
#import "NameViewController.h"
#import "BirthdayViewController.h"
#import "MeAboutViewController.h"
#import "PhotoModel.h"
@interface MeInfoViewController ()
@property (nonatomic , strong) NameViewController * nameVC;
@property (nonatomic , strong) BirthdayViewController * birthdVC;
@property (nonatomic , strong) MeAboutViewController * aboutVC;
@property  (nonatomic , strong) UserModel * model;
@end

@implementation MeInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.model = [UserModel share];
    [self SQ_UIViewForSelfView];
}

- (void)SQ_UIViewForSelfView{
    self.tabBar.backgroundColor = UIColor.whiteColor;
    self.tabBar.leadingSpace = 10;
    self.tabBar.trailingSpace = 10;
    [self.tabBar setScrollEnabledAndItemFitTextWidthWithSpacing:20 minWidth:70];
    self.tabBar.indicatorColor  = SQColor333333;
    [self.tabBar setIndicatorWidth:30 marginTop:34 marginBottom:4 tapSwitchAnimated:YES];
   [self setTabBarFrame:CGRectMake(0, TopHeight-40, SCREEN_WIDETH,40 )
       contentViewFrame:CGRectMake(0, TopHeight, SCREEN_WIDETH, SCREEN_HEIGHT-TopHeight)];
    self.tabContentView.delegate = self;
   self.tabBar.itemTitleFont = FONT_MEDIUM(14);
    self.tabBar.itemTitleSelectedFont =  FONT_MEDIUM(16);
   self.tabBar.itemTitleColor = SQColor(@"#666666");
   self.tabBar.itemTitleSelectedColor = SQColor333333;
   [self.tabContentView setContentScrollEnabled:NO tapSwitchAnimated:YES];
   self.tabContentView.loadViewOfChildContollerWhileAppear = YES;
    self.viewControllers = [[NSMutableArray alloc]initWithObjects:self.nameVC,self.birthdVC,self.aboutVC, nil];
    self.tabContentView.backgroundColor = UIColor.clearColor;
    UIView * lineView = [[UIView alloc]init];
    lineView.backgroundColor = SQHexColor(@"#000000", 0.1);
    [self.tabBar addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.tabBar);
            make.right.equalTo(self.tabBar.mas_right);
            make.height.mas_equalTo(0.4);
            make.bottom.equalTo(self.tabBar.mas_bottom).offset(-4);
    }];
    
    WS(weakSelf);
    
    [self.nameVC setBtntouchBlock:^{
        weakSelf.tabContentView.selectedTabIndex = 1;
      
    }];
    [self.birthdVC setBtntouchBlock:^{
        weakSelf.tabContentView.selectedTabIndex = 2;
    }];
    [self.aboutVC setBtntouchBlock:^ {
        [weakSelf SQ_aboutButtonClick];
    }];
}
-(void)SQ_aboutButtonClick{
    if (self.nameVC.headerBtn.SQBackimageView.image ==nil) {
        self.tabContentView.selectedTabIndex = 0;
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请设置头像!")];
        return;
    }
    if (![self.nameVC.nameTextField.text isNotEmpty]) {
        self.tabContentView.selectedTabIndex = 0;
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请设置昵称!")];
        return;
    }
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    WS(weakSelf);
    [HttpTool SQ_Image:self.nameVC.headerBtn.SQBackimageView.image successBlock:^(BaseModel * _Nonnull httpModel) {
        NSString * imageurl = [NSString stringWithFormat:@"%@",httpModel.data];
        [weakSelf SQ_updateImage:imageurl];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"网络异常请重试")];
    }];
}
-(void)SQ_updateImage:(NSString*)headerImage{
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:headerImage forKey:@"portrait"];
    [dic setObject:self.nameVC.nameTextField.text forKey:@"name"];
    [dic setObject:@(self.nameVC.page) forKey:@"gender"];
    if ([self.birthdVC.birthday.text isNotEmpty]) {
        [dic setObject:self.birthdVC.birthday.text forKey:@"birthday"];
    }
    if ([self.birthdVC.city.text isNotEmpty]) {
        [dic setObject:self.birthdVC.city.text forKey:@"city"];
    }
    if ([self.aboutVC.fsTextView.formatText isNotEmpty]) {
        [dic setObject:self.aboutVC.fsTextView.formatText forKey:@"remark"];
    }
    self.model.portrait = headerImage;
    self.model.name = self.nameVC.nameTextField.text;
    [HttpTool post:@"user/config" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        if (self.birthdVC.jsonArray.count > 1) { //有相册
            [self SQ_PhotoArray];
        }else{
            [self SQ_InfoModel];
        }
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        if ([httpModel.message isEqualToString:@"资料审核中,请耐心的等待"]) {
            [self SQ_InfoModel];
        } else {
            [YJProgressHUD showMsgWithoutView:httpModel.message];
        }
    }];
}
-(void)SQ_PhotoArray{
    NSMutableArray * imageArray = [NSMutableArray new];
    for (PhotoModel* model in self.birthdVC.jsonArray) {
        if (model.isAdd) {
            continue;
        }
        [HttpTool SQ_Image:model.addImage successBlock:^(BaseModel * _Nonnull httpModel) {
            [imageArray addObject:httpModel.data];
            if (imageArray.count+1 == self.birthdVC.jsonArray.count) {
                [self SQ_uplode:imageArray];
            }
        } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
            [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"网络异常请重试")];
        }];
    }
}
-(void)SQ_uplode:(NSArray*)imageArray{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
//    [dic setObject:imageUrl forKey:@"url"];
    [HttpTool post:@"photo/save" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [self SQ_InfoModel];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
-(void)SQ_InfoModel{
    [UserModel updateLocalUserModel:self.model];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[Manager SQ_getViewController]];
    self.view.window.rootViewController = nav;
}

/**
 *  是否能切换到指定index
 */
- (BOOL)tabContentView:(YPTabContentView *)tabConentView shouldSelectTabAtIndex:(NSUInteger)index{
    return YES;
}
- (NameViewController *)nameVC{
    if (!_nameVC) {
        _nameVC = [[NameViewController alloc]init];
        _nameVC.yp_tabItemTitle = SQNSLocalString(@"基础信息");
    }
    return _nameVC;
}
 
- (BirthdayViewController *)birthdVC{
    if (!_birthdVC) {
        _birthdVC = [[BirthdayViewController alloc]init];
        _birthdVC.yp_tabItemTitle = SQNSLocalString(@"扩展信息");
    }
    return _birthdVC;
}
- (MeAboutViewController *)aboutVC{
    if (!_aboutVC) {
        _aboutVC = [[MeAboutViewController alloc]init];
        _aboutVC.yp_tabItemTitle = SQNSLocalString(@"自我介绍");
    }
    return _aboutVC;
}



@end
